package com.jiukuaidao.library.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiukuaidao.library.R;
import com.jiukuaidao.library.util.Utils;


/**
 * Author : BinarySatan
 * Date : 2016/5/26
 * Description :
 */
public class CommDialog extends Dialog {

    private ICallBack.OneCallBack mOneCallBack;
    private ICallBack.TwoCallBack mTwoCallBack;


    public CommDialog(Context context) {
        this(context, R.style.commDialogStyle);
    }

    public CommDialog(Context context, int themeResId) {
        super(context, themeResId);
        init();
    }

    private void init() {
        this.setCanceledOnTouchOutside(false);
    }


    public void show(String msg) {
        show(msg, null);
    }

    public void show(String msg, String confirmStr) {
        show(msg, confirmStr, null);
    }

    public void show(String msg, String leftStr, String rightStr) {
        if (isShowing())
            return;
        View view;
        RelativeLayout left;
        RelativeLayout right = null;
        TextView tvLeft;
        TextView tvRight = null;

        if (mOneCallBack != null) {
            view = LayoutInflater.from(Utils.getContext()).inflate(R.layout.common_dialog_btn_one, null);
        } else {
            view = LayoutInflater.from(Utils.getContext()).inflate(R.layout.common_dialog_btn_two, null);
            right = (RelativeLayout) view.findViewById(R.id.rl_right);
            tvRight = (TextView) view.findViewById(R.id.tv_right);
        }

        left = (RelativeLayout) view.findViewById(R.id.rl_left);
        tvLeft = (TextView) view.findViewById(R.id.tv_left);

        if (!TextUtils.isEmpty(leftStr))
            tvLeft.setText(leftStr);

        if (!TextUtils.isEmpty(rightStr) && tvRight != null)
            tvRight.setText(rightStr);

        handleClick(left, right);

        TextView message = (TextView) view.findViewById(R.id.tv_dialog_message);
        message.setText(msg);

        this.setContentView(view);

        super.show();
    }


    private void handleClick(RelativeLayout left, RelativeLayout right) {
        if (mOneCallBack != null) {
            if (left != null)
                left.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOneCallBack.confirm(CommDialog.this);
                        CommDialog.this.dismiss();
                    }
                });
        }


        if (mTwoCallBack != null) {
            if (left != null)
                left.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mTwoCallBack.left(CommDialog.this);
                        CommDialog.this.dismiss();
                    }
                });
            if (right != null)
                right.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mTwoCallBack.right(CommDialog.this);
                        CommDialog.this.dismiss();
                    }
                });
        }
    }

    public void setCallback(ICallBack callback) {
        if (callback instanceof ICallBack.OneCallBack)
            mOneCallBack = (ICallBack.OneCallBack) callback;
        else if (callback instanceof ICallBack.TwoCallBack)
            mTwoCallBack = (ICallBack.TwoCallBack) callback;
    }

    @Override
    public void dismiss() {
        if(isShowing())
        super.dismiss();
    }
}
